package pers.archives.home.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pers.archives.common.entity.Menu;
import pers.archives.common.entity.User;
import pers.archives.common.ov.OVMenus;
import pers.archives.common.utils.redis.RedisConstantUtil;
import pers.archives.home.mapper.MenuMapper;
import pers.archives.home.service.MenuService;
import pers.archives.home.util.MenuUtils;

import java.util.ArrayList;
import java.util.List;

/*
 *@program:archives-parent
 *@author: 赵浩浩
 *@Time: 2020/9/19  14:43
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 获取当前用户所拥有的菜单权限
     * @param ids 菜单集合
     * @param user 当前用户
     * @return
     */
    @Override
    public List<OVMenus> getMenu(List<String> ids, User user) {
        //先从缓存中获取 如果没有读取数据库
        List<OVMenus> archiveHomeMenu = RedisConstantUtil.getArchiveHomeMenu(user.getUserName());
        if(archiveHomeMenu != null) {
            return archiveHomeMenu;
        }
        if ("admin".equals(user.getUserName())) {
            ids = null;
        }
        List<Menu> menus = menuMapper.findByIds(ids);
        if (CollectionUtils.isEmpty(menus)) {
            return null;
        }
        // 设置顶级菜单
        Menu root = new Menu();
        root.setId("root");
        //获取菜单集合
        List<OVMenus> ov = MenuUtils.splicingMenu(root, menus);
        //将查询到的数据放入缓存
        RedisConstantUtil.setArchiveHomeMenu(ov, user.getUserName());
        return ov;
    }




}
